package com.hkbigdata.sybx.mapper;

import com.hkbigdata.sybx.bean.PlayCount;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PlayCountMapper {
    // 插入新视频的播放计数
    @Insert("INSERT INTO videos (title, play_count) VALUES (#{title}, #{playCount})")
    void insertPlayCount(PlayCount playCount);

    // 定义PlayCount的结果映射
    @Results(id = "playCountResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "playCount", column = "play_count") // 明确映射play_count列到playCount属性
    })
    // 根据视频标题查询其播放计数，包含id字段
    @Select("SELECT id, title, play_count FROM videos WHERE title = #{title}")
    PlayCount selectPlayCountByTitle(String title);

    // 更新特定视频的播放计数
    @Update("UPDATE videos SET play_count = #{playCount} WHERE title = #{title}")
    int updatePlayCount(PlayCount playCount);

    // 获取所有视频的标题和播放计数，包含id字段
    // 使用已定义的结果映射
    @Results(id = "playCountsListResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "playCount", column = "play_count") // 明确映射play_count列到playCount属性
    })
    @Select("SELECT id, title, play_count FROM videos")
    List<PlayCount> selectAllPlayCounts();
}